package com.opengate.base.intercept;

import com.opengate.base.currentuser.resolver.CurrentUserResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author 曳戈泰尔
 * @version 1.0
 * @description 拦截器配置类
 * @date 2023/4/26 5:46 PM
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    /**
     * 添加拦截器
     *
     * @param registry 拦截器注册器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogInterceptor());
    }

    /**
     * 注入currentUserMethodArgumentResolver()
     *
     * @return CurrentUserResolver
     */
    @Bean
    CurrentUserResolver currentUserMethodArgumentResolver() {
        return new CurrentUserResolver();
    }

    /**
     * 指定参数解析器
     *
     * @param argumentResolvers 参数解析器列表
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserMethodArgumentResolver());
    }
}
